package com.aier.common.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

public class CountryRowMapper implements RowMapper<Country> {

    @SuppressWarnings("unused")
    private int type;

    public CountryRowMapper() {
    }

    public CountryRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Country mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseCountry(new Country(), rs);
    }

    public static Country parseCountry(Country c, ResultSet rs)
        throws SQLException {
        return parseCountry(c, rs, null);
    }

    public static Country parseCountry(Country c, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            c.setCtid(rs.getLong(su + "ctid"));
        } catch (SQLException e) {
            c.setCtid(null);
        }
        try {
            c.setName_cn(rs.getString(su + "name_cn"));
        } catch (SQLException e) {
            c.setName_cn(null);
        }
        try {
            c.setName_en(rs.getString(su + "name_en"));
        } catch (SQLException e) {
            c.setName_en(null);
        }
        try {
            c.setCode(rs.getString(su + "code"));
        } catch (SQLException e) {
            c.setCode(null);
        }
        try {
            c.setImage(rs.getString(su + "image"));
        } catch (SQLException e) {
            c.setImage(null);
        }
        return c;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Country c) {
        par.put("ctid", c.getCtid());
        par.put("name_cn", c.getName_cn());
        par.put("name_en", c.getName_en());
        par.put("code", c.getCode());
        par.put("image", c.getImage());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            Country c) {
        args.put("ctid", c.getCtid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Country c) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(c),
                        new String[] { "ctid" });
                parsePS(ps, c);
                return ps;
            }

        };
    }

    private static String parseSQL(Country c) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into country(");
        int count = 0;
        if (null != c.getCtid()) {
            sql.append("ctid,");
            count++;
        }

        if (null != c.getName_cn()) {
            sql.append("name_cn,");
            count++;
        }

        if (null != c.getName_en()) {
            sql.append("name_en,");
            count++;
        }
        
        if (null != c.getCode()) {
            sql.append("code,");
            count++;
        }

        if (null != c.getImage()) {
            sql.append("image,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Country c)
        throws SQLException {
        int count = 1;
        if (null != c.getCtid()) {
            ps.setLong(count++, c.getCtid());
        }

        if (null != c.getName_cn()) {
            ps.setString(count++, c.getName_cn());
        }

        if (null != c.getName_en()) {
            ps.setString(count++, c.getName_en());
        }

        if (null != c.getCode()) {
            ps.setString(count++, c.getCode());
        }
        
        if (null != c.getImage()) {
            ps.setString(count++, c.getImage());
        }
    }

}
